﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Library;

namespace Sport_Shop.Winform
{
    public partial class QLSanPham : Form
    {
        public QLSanPham()
        {
            InitializeComponent();
        }

        List<SanPham> ListSP = new List<SanPham>();
        List<SanPham> SPToDelete = new List<SanPham>();

        //Thêm SP mới vào Gridview
        private void SetSPMoi(SanPham sp)
        {
            ListSP.Add(sp);

            dgvSanPham.AutoGenerateColumns = false;
            dgvSanPham.DataSource = null;
            dgvSanPham.DataSource = ListSP;
        }

        //Sửa SP hiện tại vào GridView
        private void SetCurrentSP(SanPham sp)
        {
            int i = ListSP.FindIndex(x => x.ID == sp.ID);

            ListSP[i] = sp;

            dgvSanPham.DataSource = null;
            dgvSanPham.DataSource = ListSP;
        }

        //Button Thêm
        #region Button Thêm
        private void btThem_Click(object sender, EventArgs e)
        {
            QLSanPhamUpdate qlspu = new QLSanPhamUpdate();
            qlspu.flag = "new";
            qlspu.SetSP = new QLSanPhamUpdate.SetSPMoi(this.SetSPMoi);
            qlspu.ShowDialog();    
        }
        #endregion

        //Button Sửa
        #region Button Sửa
        private void btSua_Click(object sender, EventArgs e)
        {
            if (dgvSanPham.SelectedRows.Count == 1)
            {
                QLSanPhamUpdate qlspu = new QLSanPhamUpdate();
                qlspu.flag = "update";
                qlspu.CurrentSP = (SanPham)dgvSanPham.SelectedRows[0].DataBoundItem;
                qlspu.SetSP = new QLSanPhamUpdate.SetSPMoi(this.SetCurrentSP);
                qlspu.ShowDialog();
            }
        }
        #endregion

        //Button Xóa
        #region Button Xóa
        private void btXoa_Click(object sender, EventArgs e)
        {
            if (dgvSanPham.SelectedRows.Count == 1)
            {
                SanPham sp = (SanPham)dgvSanPham.SelectedRows[0].DataBoundItem;

                if (MessageBox.Show("Muốn xóa thiệt hả?", "Xác nhận xóa", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ListSP.Remove(sp);
                    SPToDelete.Add(sp);

                    dgvSanPham.DataSource = null;
                    dgvSanPham.DataSource = ListSP;
                }
            }
        }
        #endregion

        //Button Lưu
        #region Button Lưu
        private void btSave_Click(object sender, EventArgs e)
        {
            //Lưu Add và Update
            foreach (SanPham d in ListSP)
            {
                try
                {
                    if (d.ID == -1)
                        d.Add();

                    if (d.updated == true)
                        d.Update();
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            //Lưu Delete
            try
            {
                foreach (SanPham p in SPToDelete)
                    p.Delete();

                SPToDelete = new List<SanPham>();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            MessageBox.Show("Đã lưu thành công!!!");
            ListSP = SanPham.GetList();
            dgvSanPham.DataSource = null;
            dgvSanPham.DataSource = ListSP;
        }
        #endregion

        //Form Load
        #region Form Load
        private void QLSanPham_Load(object sender, EventArgs e)
        {
            //Load gridview
            ListSP = SanPham.GetList();
                
            dgvSanPham.AutoGenerateColumns = false;
            dgvSanPham.DataSource = ListSP;

            //Load 2 ComboBox
            List<LoaiSanPham> ds_Loai = LoaiSanPham.GetList();
            ds_Loai.Insert(0, new LoaiSanPham() { MaLoai = "", TenLoai = "Tất cả" });
 
            cbLoaiSP.DisplayMember = "TenLoai";
            cbLoaiSP.ValueMember = "MaLoai";
            cbLoaiSP.DataSource = ds_Loai;

            List<MonTheThao> ds_MTT = MonTheThao.GetList();
            ds_MTT.Insert(0, new MonTheThao() { MaMTT= "", TenMTT = "Tất cả" });
            
            cbMonTT.DisplayMember = "TenMTT";
            cbMonTT.ValueMember = "MaMTT";
            cbMonTT.DataSource = ds_MTT;
        }
        #endregion

        //Load hình ảnh lên khi có event CellClick
        #region Event CellClick
        private void dgvSanPham_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                SanPham sp = (SanPham)(dgvSanPham.Rows[e.RowIndex].DataBoundItem);
                string currentDir = Directory.GetCurrentDirectory();
                string path = currentDir.Remove(currentDir.Length - 29);
                try
                {
                    Image img = Image.FromFile(path + "\\Sport_Shop\\Image\\Sanpham\\" + sp.HinhAnh);
                    picBox.Image = img;
                    picBox.SizeMode = PictureBoxSizeMode.StretchImage;
                }
                catch
                {
                    picBox.Image = null;
                }
            }
        }
        #endregion

        //Lọc dữ liệu thực hiện trên 2 ComboBox
        #region Lọc dữ liệu thực hiện trên 2 ComboBox
        private void cbLoaiSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<SanPham> result = null;

            result = SanPham.FilterList(cbLoaiSP.SelectedValue.ToString(), cbMonTT.SelectedValue.ToString(), ListSP);
            
            dgvSanPham.AutoGenerateColumns = false;
            dgvSanPham.DataSource = null;
            dgvSanPham.DataSource = result;
        }
        #endregion
    }
}
